﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports Utilerias
Imports WSFactory
Imports MySql.Data.MySqlClient
Public Class ComplementosDAL
    Private cadConex As String = ""

    Public Sub New(ByVal pCadConex As String)
        Me.cadConex = pCadConex
    End Sub

#Region "FuncionesUtilizables"

    Public Function actualizaComplemento(ByVal pComplementos As ComplementosBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE complementos " & _
                                         "SET Status = @Status, RegimenFiscal = @RegimenFiscal " & _
                                         "WHERE CveComp = @CveComp; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@Status", Conversiones.convertBoolToSN(pComplementos.Activo))
                    myComm.Parameters.AddWithValue("@RegimenFiscal", pComplementos.RegimenFiscal)
                    myComm.Parameters.AddWithValue("@CveComp", pComplementos.CveComp)
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function getComplementos(ByVal pClve As String) As ComplementosBE
        Dim tmpComplementos As ComplementosBE
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT Id, CveComp, Status, RegimenFiscal " & _
                                         "FROM complementos " & _
                                         "WHERE CveComp = @CveComp;"
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddWithValue("@CveComp", pClve)
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpComplementos = ComplementosFact.Build(myDR)
                        Else
                            tmpComplementos = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpComplementos

        Catch ex As Exception
            Throw
        Finally
            tmpComplementos = Nothing
        End Try
    End Function

#End Region

End Class
